package net.rgehan.Cloth;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.plugin.java.JavaPlugin;

public class Cloth extends JavaPlugin
{
    public String name = "Cloth";
    public String ver  = "0.1";
    public Logger log  = Logger.getLogger("Minecraft");
    public ArrayList<String[]> names = new ArrayList<String[]>();
    private String dir = "plugins/Cloth/";
	public static String propertiesFile = "plugins/Cloth/Cloth.properties";
	static PluginProperties config;
	String colors[] = new String[16];
	String right, info;
	String[] infos;
    //public ClothPlayerListener playerListener = new ClothPlayerListener(this);


    public void onEnable()
    {
        log.log(Level.INFO, "[" + name + "] " + name + " " + ver + " enabled successfully.");
        config = new PluginProperties(propertiesFile, dir);
		config.load();
		config.save();
		setColors();
		right = config.getString("right", "You haven't got the right to use /cloth !");
		info = config.getString("info", "You just received a stack of <color> wool !");
		String delimiter = "<color>";
		infos = info.split(delimiter);
	}

    public void onDisable()
    {
        log.log(Level.INFO, "[" + name + "] " + name + " " + ver + " disabled successfully.");
    }
    
    public Player getThePlayer(String name)
    {
        for(Player p : getServer().getOnlinePlayers())
        {
            if(p.getDisplayName().equals(name))
            {
                return p;
            }
        }
        return getServer().getPlayer(name);
    }
    
    public void setColors()
    {
    	//Different colors for wool
    	String[] color = {"white", "orange", "magenta", "light_blue","yellow","light_green","pink","gray","light_gray","cyan","purple","blue", "brown","dark_green","red","black"};
    
    	for(int i = 0; i<16; i++)
    	{
    		colors[i] = config.getString(color[i], color[i]);
    	}
    }
    
    
    public int getColorId(String input)
    {
    	int i = 0;
    	
    	do
    	{
    		
    		if(colors[i].equalsIgnoreCase(input))
    		{
    			return i;
    		}
  
    		i++;
    	}
    	while(i < 16);
    	
		return 0;
    }
    
    public String getColorId(String input, boolean b)
    {
    	int i = 0;
    	
    	while(i < 16)
    	{
    		
    		if(colors[i].equalsIgnoreCase(input) && b == true)
    		{
    			return colors[i];
    		}
  
    		i++;
    	}
    	
    	
		return colors[0];
    }
    
    
    @Override
    public boolean onCommand(CommandSender cs, Command c, String command, String[] args)
    {
       String[] split = args;
       String commandName = c.getName().toLowerCase();
       
       if(cs instanceof Player)
       {
    	   Player pl = (Player) cs;
    	   
    	   if(commandName.equals("cloth"))
    	   {   
    		   if(pl.isOp())
    		   {
    			   if(split.length == 1)
    			   {
    				  PlayerInventory inventory = pl.getInventory();
    				  ItemStack stack = null;
    				  int i = 0;
    				  boolean next = true;
    				  
    				  while(i<40 && next == true)
    				  {
    					  //Si on peut mettre dans l'inventaire
    					  if(i<36)
    					  {
    						  stack = inventory.getItem(i);
    						  //Si le slot est vide
    						  if(stack.getTypeId() == 0)
    						  {    							  
    							  stack.setTypeId(35);
    							  stack.setDurability((short)getColorId(split[0]));
    							  stack.setAmount(64);
    							  inventory.setItem(i, stack);
    							  pl.updateInventory();
    							  next = false;
    							  pl.sendMessage(ChatColor.GREEN+infos[0]+getColorId(split[0], true)+infos[1]);
    							  return true;
    						  }
    					  }
    					  i++;
    				  }
    			   }
    		   }
    		   else
    		   {
    			   cs.sendMessage(ChatColor.RED + right);
    			   return true;
    		   }
    	   }
       }
       return false;
    }
}
